//
//  NRWCarSearchView.m
//  New_Runway
//
//  Created by 邢毅 on 2018/4/20.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarSearchView.h"
//#import "NRWCarRegularTool.h"

@interface NRWCarSearchView()<UITextFieldDelegate>

@end

@implementation NRWCarSearchView
{
    UIButton *_leftItemBtn;
    UIButton *_rightItemBtn;
}

- (instancetype)initWithPlaceHold:(NSString *)placeHold
                    leftImageName:(NSString *)leftImageName
                   rightImageName:(NSString *)rightImageName {
    
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.tintColor = MAIN_NAV_COLOR();
        self
        .setPlaceholder(placeHold)
        .setTextColor([UIColor colorFromHexCode:@"#323232"])
        .setFont(kPingFangRegular13())
        .setReturnKeyType(UIReturnKeySearch);
        
        if (getImageFromName(leftImageName)) {
            
            [self addLeftItemBtnWithImageName:leftImageName];
        }
        
        if (getImageFromName(rightImageName)) {
            
            [self addRightItemBtnWithImageName:rightImageName];
        }
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
        
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _leftItemBtn.frame = CGRectMake(0, 0, self.viewHeight, self.viewHeight);
    _rightItemBtn.frame = CGRectMake(self.viewWidth - self.viewHeight, 0, self.viewHeight, self.viewHeight);
}

- (void)addLeftItemBtnWithImageName:(NSString *)name {
    
    _leftItemBtn = DPButton().setNormalImage(getImageFromName(name)).addClickAction(self, @selector(leftItemBtnDidClick:));
    self.leftView = _leftItemBtn;
    self.leftViewMode = UITextFieldViewModeAlways;
}

- (void)addRightItemBtnWithImageName:(NSString *)name {
    
    _rightItemBtn = DPButton().setNormalImage(getImageFromName(name)).addClickAction(self, @selector(rightItemBtnDidClick:));
    self.rightView = _rightItemBtn;
    self.rightViewMode = UITextFieldViewModeWhileEditing;
}

- (void)leftItemBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate_custom respondsToSelector:@selector(searchView:leftBtnDidClick:)]) {
        
        [self.delegate_custom searchView:self leftBtnDidClick:sender];
    }
}

- (void)rightItemBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate_custom respondsToSelector:@selector(searchView:rightBtnDidClick:)]) {
        
        [self.delegate_custom searchView:self rightBtnDidClick:sender];
    }
}

- (void)textDidChange:(NSNotification *)notification {
    
    if ([self.delegate_custom respondsToSelector:@selector(searchView:textDidChange:)]) {
        
        [self.delegate_custom searchView:self textDidChange:self.text];
    }
}

@end
